<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.12"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>emp-toolkit: /Users/wangxiao/git/emp-toolkit/emp-tool/io/mem_io_channel.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo-full-doxygen.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">emp-toolkit
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.12 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('mem__io__channel_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">mem_io_channel.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="mem__io__channel_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="io__channel_8h.html">io_channel.h</a>&quot;</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="file__io__channel_8h.html">file_io_channel.h</a>&quot;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#ifndef MEM_IO_CHANNEL</span></div><div class="line"><a name="l00006"></a><span class="lineno"><a class="line" href="mem__io__channel_8h.html#a7abfcdb3a2ce97170542c3dd3819c67c">    6</a></span>&#160;<span class="preprocessor">#define MEM_IO_CHANNEL</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div><div class="line"><a name="l00012"></a><span class="lineno"><a class="line" href="class_mem_i_o.html">   12</a></span>&#160;<span class="keyword">class </span><a class="code" href="class_mem_i_o.html">MemIO</a>: <span class="keyword">public</span> <a class="code" href="class_i_o_channel.html">IOChannel</a>&lt;MemIO&gt; { <span class="keyword">public</span>:</div><div class="line"><a name="l00013"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">   13</a></span>&#160;    <span class="keywordtype">char</span> * <a class="code" href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">buffer</a> = <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00014"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">   14</a></span>&#160;    <span class="keywordtype">int</span> <a class="code" href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">size</a> = 0;</div><div class="line"><a name="l00015"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a04ead8c796e73c9fe035736a898ad022">   15</a></span>&#160;    <span class="keywordtype">int</span> <a class="code" href="class_mem_i_o.html#a04ead8c796e73c9fe035736a898ad022">read_pos</a> = 0;</div><div class="line"><a name="l00016"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#af647b1c4977d3c961b144c43132dd8b5">   16</a></span>&#160;    <span class="keywordtype">int</span> <a class="code" href="class_mem_i_o.html#af647b1c4977d3c961b144c43132dd8b5">cap</a>;</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a52cb0fe5f80a4f19bbe3267751cda415">   18</a></span>&#160;    <a class="code" href="class_mem_i_o.html#a52cb0fe5f80a4f19bbe3267751cda415">MemIO</a>(<span class="keywordtype">int</span> _cap=1024*1024) {</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;        this-&gt;cap = _cap;</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        buffer = <span class="keyword">new</span> <span class="keywordtype">char</span>[<a class="code" href="class_mem_i_o.html#af647b1c4977d3c961b144c43132dd8b5">cap</a>];</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        size = 0;</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    }</div><div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a2bc743e6a7c8306d4f7e2ed6f49a6f1b">   23</a></span>&#160;    <a class="code" href="class_mem_i_o.html#a2bc743e6a7c8306d4f7e2ed6f49a6f1b">~MemIO</a>(){</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;        <span class="keywordflow">if</span>(buffer!=<span class="keyword">nullptr</span>)</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;            <span class="keyword">delete</span>[] <a class="code" href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">buffer</a>;</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;    }</div><div class="line"><a name="l00027"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#adb9107743ab997d3861bd2347c48498a">   27</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_mem_i_o.html#adb9107743ab997d3861bd2347c48498a">load_from_file</a>(<a class="code" href="class_file_i_o.html">FileIO</a> * fio, uint64_t size) {</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;        <span class="keyword">delete</span>[] <a class="code" href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">buffer</a>;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        buffer = <span class="keyword">new</span> <span class="keywordtype">char</span>[<a class="code" href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">size</a>];</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;        this-&gt;cap = <a class="code" href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">size</a>;</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;        this-&gt;read_pos = 0;</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;        this-&gt;size = <a class="code" href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">size</a>;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;        fio-&gt;<a class="code" href="class_i_o_channel.html#aecdc2e6d7e95d96d346c63fc57e5b4eb">recv_data</a>(buffer, size);</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;    }</div><div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a18869b6fa0e4b8ae87dbbba9651a9065">   35</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_mem_i_o.html#a18869b6fa0e4b8ae87dbbba9651a9065">clear</a>(){</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;        size = 0;</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    }</div><div class="line"><a name="l00038"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#a7ae75e4a15fdd39673cd3a07d510b2bd">   38</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_mem_i_o.html#a7ae75e4a15fdd39673cd3a07d510b2bd">send_data_impl</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> * data, <span class="keywordtype">int</span> len) {</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        <span class="keywordflow">if</span>(size + len &gt;= cap){</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;            <span class="keywordtype">char</span> * new_buffer = <span class="keyword">new</span> <span class="keywordtype">char</span>[2*(cap+len)];</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;            memcpy(new_buffer, buffer, size);</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;            <span class="keyword">delete</span>[] <a class="code" href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">buffer</a>;</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;            buffer = new_buffer;</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;            cap = 2*(cap + len);</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;        }</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;        memcpy(buffer + size, data, len);</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;        size += len;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    }</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;</div><div class="line"><a name="l00050"></a><span class="lineno"><a class="line" href="class_mem_i_o.html#acbf4dfb021efcb9285855cac8e3c8a69">   50</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_mem_i_o.html#acbf4dfb021efcb9285855cac8e3c8a69">recv_data_impl</a>(<span class="keywordtype">void</span>  * data, <span class="keywordtype">int</span> len) {</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;        <span class="keywordflow">if</span>(read_pos + len &lt;= size) {</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;            memcpy(data, buffer + read_pos, len);</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;            read_pos += len;</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;        } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;            fprintf(stderr,<span class="stringliteral">&quot;error: mem_recv_data\n&quot;</span>);</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;        }</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    }</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;};</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;<span class="preprocessor">#endif//MEM_IO_CHANNEL_H__</span></div><div class="ttc" id="class_mem_i_o_html_a52cb0fe5f80a4f19bbe3267751cda415"><div class="ttname"><a href="class_mem_i_o.html#a52cb0fe5f80a4f19bbe3267751cda415">MemIO::MemIO</a></div><div class="ttdeci">MemIO(int _cap=1024 *1024)</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:18</div></div>
<div class="ttc" id="class_i_o_channel_html_aecdc2e6d7e95d96d346c63fc57e5b4eb"><div class="ttname"><a href="class_i_o_channel.html#aecdc2e6d7e95d96d346c63fc57e5b4eb">IOChannel::recv_data</a></div><div class="ttdeci">void recv_data(void *data, int nbyte)</div><div class="ttdef"><b>Definition:</b> io_channel.h:17</div></div>
<div class="ttc" id="class_mem_i_o_html"><div class="ttname"><a href="class_mem_i_o.html">MemIO</a></div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:12</div></div>
<div class="ttc" id="io__channel_8h_html"><div class="ttname"><a href="io__channel_8h.html">io_channel.h</a></div></div>
<div class="ttc" id="class_mem_i_o_html_a7ae75e4a15fdd39673cd3a07d510b2bd"><div class="ttname"><a href="class_mem_i_o.html#a7ae75e4a15fdd39673cd3a07d510b2bd">MemIO::send_data_impl</a></div><div class="ttdeci">void send_data_impl(const void *data, int len)</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:38</div></div>
<div class="ttc" id="class_mem_i_o_html_adb9107743ab997d3861bd2347c48498a"><div class="ttname"><a href="class_mem_i_o.html#adb9107743ab997d3861bd2347c48498a">MemIO::load_from_file</a></div><div class="ttdeci">void load_from_file(FileIO *fio, uint64_t size)</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:27</div></div>
<div class="ttc" id="class_mem_i_o_html_a2bc743e6a7c8306d4f7e2ed6f49a6f1b"><div class="ttname"><a href="class_mem_i_o.html#a2bc743e6a7c8306d4f7e2ed6f49a6f1b">MemIO::~MemIO</a></div><div class="ttdeci">~MemIO()</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:23</div></div>
<div class="ttc" id="class_mem_i_o_html_af647b1c4977d3c961b144c43132dd8b5"><div class="ttname"><a href="class_mem_i_o.html#af647b1c4977d3c961b144c43132dd8b5">MemIO::cap</a></div><div class="ttdeci">int cap</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:16</div></div>
<div class="ttc" id="class_mem_i_o_html_acbf4dfb021efcb9285855cac8e3c8a69"><div class="ttname"><a href="class_mem_i_o.html#acbf4dfb021efcb9285855cac8e3c8a69">MemIO::recv_data_impl</a></div><div class="ttdeci">void recv_data_impl(void *data, int len)</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:50</div></div>
<div class="ttc" id="class_mem_i_o_html_a18869b6fa0e4b8ae87dbbba9651a9065"><div class="ttname"><a href="class_mem_i_o.html#a18869b6fa0e4b8ae87dbbba9651a9065">MemIO::clear</a></div><div class="ttdeci">void clear()</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:35</div></div>
<div class="ttc" id="class_mem_i_o_html_a04ead8c796e73c9fe035736a898ad022"><div class="ttname"><a href="class_mem_i_o.html#a04ead8c796e73c9fe035736a898ad022">MemIO::read_pos</a></div><div class="ttdeci">int read_pos</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:15</div></div>
<div class="ttc" id="class_mem_i_o_html_a45e316707c58099814940fb636b3eaa5"><div class="ttname"><a href="class_mem_i_o.html#a45e316707c58099814940fb636b3eaa5">MemIO::size</a></div><div class="ttdeci">int size</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:14</div></div>
<div class="ttc" id="class_mem_i_o_html_a6b392876ae789bfd4ea032c582dc9153"><div class="ttname"><a href="class_mem_i_o.html#a6b392876ae789bfd4ea032c582dc9153">MemIO::buffer</a></div><div class="ttdeci">char * buffer</div><div class="ttdef"><b>Definition:</b> mem_io_channel.h:13</div></div>
<div class="ttc" id="file__io__channel_8h_html"><div class="ttname"><a href="file__io__channel_8h.html">file_io_channel.h</a></div></div>
<div class="ttc" id="class_i_o_channel_html"><div class="ttname"><a href="class_i_o_channel.html">IOChannel</a></div><div class="ttdef"><b>Definition:</b> io_channel.h:12</div></div>
<div class="ttc" id="class_file_i_o_html"><div class="ttname"><a href="class_file_i_o.html">FileIO</a></div><div class="ttdef"><b>Definition:</b> file_io_channel.h:21</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_eec959d81771dc8b1a750b834e1341eb.html">emp-tool</a></li><li class="navelem"><a class="el" href="dir_6f7196b439314a1a73bb5510dd78598a.html">io</a></li><li class="navelem"><a class="el" href="mem__io__channel_8h.html">mem_io_channel.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.12 </li>
  </ul>
</div>
</body>
</html>
